package com.raymond.controller;

import com.raymond.pojo.Member;
import com.raymond.pojo.User;
import com.raymond.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 用户信息
 * @Author: yc
 * @CreateDate: 2019/2/18$ 16:24$
 * @Version: 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    /**
     * 查询所有用户
     * @return
     */
    @RequestMapping("/find")
    public List<User> findUser(){
//        Member vo=new Member();
//        vo.setMid("studyjava");
//        vo.setAge(19);
//        this.redisTemplate.opsForValue().set("study", vo);
        System.out.println(this.redisTemplate.opsForValue().get("study"));
        return userService.findUser();
    }

    /**
     * 添加用户
     * @return
     */
    @RequestMapping("/register")
    public String insertUser(User user){
        try {
            if(userService.getUser(user.getName())!=null){
                return "exist";
            }
            if(userService.insertSelective(user)){
                return "success";
            }
            return "fail";
        }catch (Exception e){
            return "error";
        }
    }
    @RequestMapping("/login")
    public String login(User user){
        try {
            if(userService.login(user)!=null){
                return "success";
            }
            return "fail";
        }catch (Exception e){
            return "error";
        }
    }
}
